package com.minhua.jijiehao2.mainframe.account.addaccountactivity.impl

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.text.TextUtils
import android.view.View
import com.bumptech.glide.Glide
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.bean.AccountBean
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.mainframe.account.addaccountactivity.imp.IAddAccountView
import com.minhua.jijiehao2.mainframe.main.impl.MainActivity
import com.minhua.jijiehao2.utils.AppUtils
import com.minhua.jijiehao2.utils.SPUtils
import com.minhua.jijiehao2.utils.T
import com.minhua.jijiehao2.view.OptionsPickerViewUtil
import kotlinx.android.synthetic.main.activity_add_account.*
import kotlinx.android.synthetic.main.layout_common_title.*
import java.text.SimpleDateFormat

class AddAccountActivity : BaseActivity<IAddAccountView, AddAccountPresenter>(), IAddAccountView {

    override fun getLayoutId(): Int = R.layout.activity_add_account

    override fun getPresenter(): AddAccountPresenter = AddAccountPresenter()

    companion object {
        fun intentStart(context: Context, pCode: String, picUrl: String) {
            val intent = Intent(context, AddAccountActivity::class.java)
            intent.putExtra("pCode", pCode)
            intent.putExtra("picUrl", picUrl)
            context.startActivity(intent)
        }

        fun intentStart(context: Context, accountBean: AccountBean) {
            val intent = Intent(context, AddAccountActivity::class.java)
            intent.putExtra("accountBean", accountBean)
            context.startActivity(intent)
        }
    }

    private val typeList = arrayListOf("一次性付款", "分期付款")

    //条件选择器
    private val mOnClickListener = View.OnClickListener { v ->
        AppUtils.hideSoftInput(this, tvTitle)
        when (v.id) {
            R.id.tvAccountType -> OptionsPickerViewUtil.getInstance().showType()
            R.id.tvAccountDate -> OptionsPickerViewUtil.getInstance().showTimer()
            R.id.tvAccountNotice -> OptionsPickerViewUtil.getInstance().showNotice()
            R.id.tvSubmit -> mPresenter.addAccount()
        }
    }


    @SuppressLint("SimpleDateFormat")
    override fun initView() {
        val accountBean = intent.getSerializableExtra("accountBean")
        if (accountBean == null) {
            initAddView()
        } else {
            initShowView(accountBean as AccountBean)
        }
    }


    /**
     *  初始化界面为展示界面
     */
    private fun initShowView(accountBean: AccountBean) {
        Glide.with(this).load(accountBean.picUrl).into(ivChannelIcon)
        llInfo.visibility = View.GONE
        tvNotice.visibility = View.GONE
        tvAccountType.viewHolder.setVisible(R.id.ivRight, View.INVISIBLE)
        tvAccountDate.viewHolder.setVisible(R.id.ivRight, View.INVISIBLE)
        tvAccountNotice.viewHolder.setVisible(R.id.ivRight, View.INVISIBLE)

        tvUserName.centerText = accountBean.userName
        tvOrderName.centerText = accountBean.ordersName
        tvAmtByMonth.centerText = accountBean.amtByMonth.toString()
        tvAccountType.centerText = if (accountBean.repayType == 0) {
            "一次性付款"
        } else {
            "分期付款"
        }
        tvTotalPeriods.centerText = accountBean.totalPeriods.toString()
        tvCurrentPeriods.centerText = accountBean.currentPeriods.toString()
        tvAccountDate.centerText = accountBean.repayRemind
        tvAccountNotice.centerText = accountBean.repayDay
    }

    /**
     * 初始化界面为添加界面
     */
    private fun initAddView() {
        val picUrl = intent.getStringExtra("picUrl")
        if (!TextUtils.isEmpty(picUrl)) {
            Glide.with(this).load(picUrl).into(ivChannelIcon)
        }

        OptionsPickerViewUtil.getInstance().initOptionsPickerView(this, typeList, { i, _, _, _ ->
            tvAccountType.centerTextView.text = typeList[i]
        })
        OptionsPickerViewUtil.getInstance().initTimerPickerView(this, { date, _ ->
            val format = SimpleDateFormat("每月dd号")
            tvAccountDate.centerTextView.text = format.format(date)
        })
        OptionsPickerViewUtil.getInstance().initNoticeView(this, { date, _ ->
            val format = SimpleDateFormat("MM-dd HH:mm")
            tvAccountNotice.centerTextView.text = format.format(date)
        })
        tvAccountType.setOnClickListener(mOnClickListener)
        tvAccountDate.setOnClickListener(mOnClickListener)
        tvAccountNotice.setOnClickListener(mOnClickListener)
        tvSubmit.setOnClickListener(mOnClickListener)
    }

    override fun pCode(): String = intent.getStringExtra("pCode")

    override fun userId(): String = SPUtils.get(DefineKey.USER_ID, "") as String

    override fun userName(): String = tvUserName.text.trim()

    override fun ordersName(): String = tvOrderName.text.trim()

    override fun amtByMonth(): String = tvAmtByMonth.text.trim()

    /**
     * 还款类型（0一次性还款，1分期还款）
     */
    override fun repayType(): String = if (tvAccountType.centerText.trim() == "一次性还款") {
        "0"
    } else {
        "1"
    }

    override fun totalPeriods(): String = tvTotalPeriods.text.trim()

    override fun currentPeriods(): String = tvCurrentPeriods.text.trim()

    override fun repayDay(): String = tvAccountDate.centerText.trim()

    override fun repayRemind(): String = tvAccountNotice.centerText.trim()

    override fun repayStatus(): String = "0"

    override fun onAddSuccess() {
        T.showShort("添加成功")
        Handler().postDelayed({ startActivity(Intent(this, MainActivity::class.java)) }, 500)
    }
}
